var roleType,unitId;
var allUnitNum = 0;
var allEquipNum = 0;
var onlineEquipNum = 0;
var onlineUnitNum = 0;
var noOnLineUnitNum = 0;
var noOnLineEquipNum = 0;

mui.init({
	pullRefresh: {
		container: '#pullrefresh',
		down: { //下拉刷新
			callback: pulldownRefresh,
			style: mui.os.android ? "circle" : "default"
		}
	},
	beforeback: function() {
		appPage.closeLogin();
	}
})

var page = 1; //初始页码
var pageCount = 0; //总页数


mui.plusReady(function() {
	unitId = localStorage.getItem("unitId");
	roleType = localStorage.getItem("roleType");
	getData();
	if(roleType==2){
		document.getElementById('jumpToScane').style.display = 'block'
	}
	//显示跳转链接
	mui('.mui-collapse-content').on('tap', 'span', function() {
		//var href=this.getAttribute('data-href');
		openNew("monitorList.html");
	})
	//跳转扫码
	document.getElementById('jumpToScane').addEventListener('tap', function() {
		openNew("../widget/richScan.html");
	})
	//跳转至所有列表
	// document.getElementById('jumpToAll').addEventListener('tap', function() {
	// 	openNew("unitAll.html");
	// })
	//跳转至查岗
	/*document.getElementById('jumpToCheckup').addEventListener('tap', function() {
		openNew("checkup.html");
	})*/
	
	mui('#list_wrap').on('tap','.unit-box',function(){
		var num = this.getAttribute("data-num");
		var unitType = this.getAttribute("data-type");
		var unitTypeName = this.getAttribute("data-href");
		if(num > 0) {
			openNew("monitorList.html",{
				unitType: unitType,
				unitTypeName: unitTypeName
			});
		}else {
			mui.toast('暂无数据')
		}
	})
})

//下拉刷新具体业务实现
function pulldownRefresh() {
	/*setTimeout(function() {
		//下拉刷新结束
		getList();
		mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
		//重置上拉加载
		mui('#pullrefresh').pullRefresh().refresh(true);
		//重置页码
		page = 1;
	}, 1500)*/
	getData()
}


function getData(){
	request("/app/alarm/select/center/number", "GET",{
		roleType: roleType,
		unitId: unitId
	}, function(res) {
		appPage.endPullRefresh();
		allEquipNum = res.result.allEquipNum;
		onlineEquipNum = res.result.onlineEquipNum;
		allUnitNum = res.result.allUnitNum;
		onlineUnitNum = res.result.onlineUnitNum;
		noOnLineUnitNum = allUnitNum - onlineUnitNum;
		noOnLineEquipNum = allEquipNum - onlineEquipNum;
		//初始化图表
		var option = {
			tooltip: {
				trigger: 'item',
				show: false
			},
			series: [{
					name: '在线单位',
					type: 'pie',
					radius: ['60%', '70%'],
					avoidLabelOverlap: false,
					label: {
						show: false,
		                normal: {
		                	formatter: '',
		                    position: 'center',
		                    textStyle: {
					            fontSize: '18',
					            fontWeight: 'bold',
					            color: '#01babc'
					        }
		                }
		            },
					data: [{
						value: onlineUnitNum,
						name: String(allUnitNum),
						itemStyle: {
							normal: {
								color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
		                                offset: 0,
		                                color: '#99da69'
		                            }, {
		                                offset: 1,
		                                color: '#01babc'
		                            }])
							},
						},
					}, {
						value: noOnLineUnitNum,
						name: '',
						label: {
							show: false,
						},
						labelLine: {
							show: false
						},
						itemStyle: {
							normal: {
								color: '#fff'
							}
						}
					}],
				},
				{
					name: '在线设备',
					type: 'pie',
					radius: ['45%', '55%'],
					avoidLabelOverlap: false,
					label: {
						show: false,
		                normal: {
		                	formatter: '',
		                    position: 'center',
		                    textStyle: {
					            fontSize: '18',
					            fontWeight: 'bold',
					            color: '#9f3edd'
					        }
		                }
		            },
					data: [{
						value: onlineEquipNum,
						name: String(allEquipNum),
						itemStyle: {
							normal: {
								color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
		                                offset: 0,
		                                color: '#9f3edd'
		                            }, {
		                                offset: 1,
		                                color: '#4897f6'
		                            }])
							}
						},
					}, {
						value: noOnLineEquipNum,
						name: '',
						label: {
							show: false,
						},
						labelLine: {
							show: false
						},
						itemStyle: {
							normal: {
								color: '#fff'
							}
						}
					}],
				}
			]
		};
		var pieChart = echarts.init(document.getElementById('pieChart'));
		pieChart.setOption(option);
		
		document.getElementById('allEquipNum').innerText = allEquipNum;
		document.getElementById('innerAllEquip').innerText = allEquipNum;
		document.getElementById('onlineEquipNum').innerText = onlineEquipNum;
		document.getElementById('allUnitNum').innerText = allUnitNum;
		document.getElementById('innerAllUnit').innerText = allUnitNum;
		document.getElementById('onlineUnitNum').innerText = onlineUnitNum;
		
		render('#list_wrap', 'monitorUnit', res.result)
		
	}, true, function() {
		appPage.endPullRefresh();
	});
}
